<?php
/**
 * WithdrawController.php
 *
 * @copyright  2023 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     TL <mengwb@guangda.work>
 * @created    2023-06-02 18:57:56
 * @modified   2023-06-02 18:57:56
 */

namespace Plugin\MultiSeller\Http\Controllers\Admin;

use Beike\Admin\Http\Controllers\Controller;
use Beike\Seller\Repositories\WithdrawRepo;
use Beike\Seller\Services\WithdrawService;
use Illuminate\Http\Request;
use Illuminate\View\View;

class WithdrawController extends Controller
{
    /**
     * @param Request $request
     * @return View
     */
    public function index(Request $request): View
    {
        $withdraws = WithdrawRepo::list();
        $data      = [
            'withdraws' => $withdraws,
        ];

        return view('MultiSeller::admin.withdraws.index', $data);
    }

    /**
     * @param int $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function edit(int $id)
    {
        $data = [
            'id'           => $id,
            'withdraw'     => WithdrawRepo::find($id),
            '_redirect'    => $this->getRedirect(),
        ];

        return view('MultiSeller::admin.withdraws.form', $data);
    }

    /**
     * @param Request $request
     * @param int     $id
     * @return array|\Illuminate\Http\RedirectResponse
     */
    public function process(Request $request, int $id)
    {
        try {
            WithdrawService::process($id, $request->get('paid'), $request->get('comment'));
        } catch (\Exception $e) {
            return json_fail($e->getMessage());
        }

        return redirect()->to(admin_route('withdraws.index'));
    }
}
